#
# This file is a part of the open-eBackup project.
# This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
# If a copy of the MPL was not distributed with this file, You can obtain one at
# http://mozilla.org/MPL/2.0/.
#
# Copyright (c) [2024] Huawei Technologies Co.,Ltd.
#
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
# EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
# MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
#

from common.exception.common_exception import ErrCodeException
from generaldb.saphana.comm.saphana_const import SaphanaErrorCode, SaphanaJsonConstant


class SaphanaResourceParam:
    @staticmethod
    def check_custom_params(custom_params: str, model_list: list):
        """
        检查自定义参数是否符合预期
        :param custom_params: 自定义参数 eg: systemId=x00,systemDbPort=30013
        :param model_list: 模板key列表
        :return:
        """
        if not custom_params:
            return
        custom_params_arr = custom_params.split(",")
        if len(custom_params_arr) != len(model_list):
            raise ErrCodeException(SaphanaErrorCode.ERROR_PARAM, "custom param len error")
        for custom_params_ele in custom_params_arr:
            if not custom_params_ele:
                raise ErrCodeException(SaphanaErrorCode.ERROR_PARAM, "custom param error")
            custom_params_ele_arr = custom_params_ele.split("=")
            if len(custom_params_ele_arr) != 2:
                raise ErrCodeException(SaphanaErrorCode.ERROR_PARAM, "custom param error")
            for custom_param_ele in custom_params_ele_arr:
                if not custom_param_ele:
                    raise ErrCodeException(SaphanaErrorCode.ERROR_PARAM, "custom param error")
        for model_ele in model_list:
            if custom_params.find(model_ele + "=") == -1:
                raise ErrCodeException(SaphanaErrorCode.ERROR_PARAM, "custom param error")

    @staticmethod
    def check_param_is_contain_space(params: list):
        for param in params:
            if not param:
                continue
            if param.strip().find(" ") != -1:
                raise ErrCodeException(SaphanaErrorCode.ERROR_PARAM, message="custom param contain space")

    @staticmethod
    def init_custom_params(custom_params: str):
        custom_param_arr = custom_params.split(",")
        custom_param_dict = {}
        for custom_param_ele in custom_param_arr:
            if not custom_param_ele:
                continue
            custom_param_ele_arr = custom_param_ele.split("=")
            if len(custom_param_ele_arr) != 2:
                raise ErrCodeException(SaphanaErrorCode.ERROR_PARAM, "custom param len error.")
            if custom_param_ele_arr[0] == SaphanaJsonConstant.SYSTEM_DB_USER:
                custom_param_dict[SaphanaJsonConstant.SYSTEM_DB_USER] = custom_param_ele_arr[1]
            if custom_param_ele_arr[0] == SaphanaJsonConstant.SYSTEM_DB_PASSWORD:
                custom_param_dict[SaphanaJsonConstant.SYSTEM_DB_PASSWORD] = custom_param_ele_arr[1]
            if custom_param_ele_arr[0] == SaphanaJsonConstant.SYSTEM_ID:
                custom_param_dict[SaphanaJsonConstant.SYSTEM_ID] = custom_param_ele_arr[1]
            if custom_param_ele_arr[0] == SaphanaJsonConstant.SYSTEM_DB_PORT:
                custom_param_dict[SaphanaJsonConstant.SYSTEM_DB_PORT] = custom_param_ele_arr[1]
        return custom_param_dict



